<?php

declare(strict_types=1);

namespace App\CustHub\Model;

use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Relations\HasOne;
use Hyperf\Database\Model\SoftDeletes;
use App\CustHub\Policies\ModelPolicy;
use Hyperf\Database\Model\Relations\BelongsToMany;
use Hyperf\Di\Annotation\Inject;
use Mine\MineModel;

/**
 * @property int $id 
 * @property int $uid 私域用户ID
 * @property int $main_uid 主账号ID
 * @property string $nickname 昵称
 * @property string $acctid 
 * @property string $alias 别名
 * @property string $real_name 真实姓名
 * @property string $remark 备注信息
 * @property string $avatar 头像
 * @property string $mobile 手机号
 * @property int $corp_id 企业ID
 * @property string $corp_name 企业名称
 * @property string $corp_wxid 企业微信ID
 * @property string $user_id 微信ID
 * @property string $userid 用户openid
 * @property int $login_status 登录状态：0=未登录 1=已登录
 * @property string $first_login_time 首次登录时间
 * @property string $login_time 登录时间
 * @property string $login_out_time 退出登录时间
 * @property int $group_id 分组ID：0=未分组
 * @property string $pro_code 省份编码
 * @property string $pro_name 
 * @property string $city_code 城市编码
 * @property string $city_name 
 * @property string $server_pro_code 服务器所在省份
 * @property string $server_city_code 服务器所在城市
 * @property string $server_pro_name 
 * @property string $server_city_name 
 * @property int $is_yd 是否异地登录
 * @property int $seat_id 席位id
 * @property string $robot_key 机器人key
 * @property int $server_id 机器人所在服务器ID
 * @property int $contact_num 客户数
 * @property int $room_num 群数量
 * @property int $is_auto_renewal 是否自动续费：0=否 1=是
 * @property string $uuid 设备ID
 * @property int $ht_bind_status 互通账号绑定状态：0=未绑定 1=已绑定
 * @property int $ht_seat_id 互通账号席位id
 * @property int $is_api 是否接口创建
 * @property string $deleted_at 删除时间
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 更新时间
 * @property int $gender 性别：0=未知 1=男 2=女
 * @property-read null|\Hyperf\Database\Model\Collection|ChRobotGroup[] $groups 
 * @property-read null|ChUser $user 
 * @property-read null|ChRoomInviteAcceptConfig $roomInviteAcceptConfig 
 * @property-read null|ChServer $server 
 */
class ChRobot extends MineModel
{
    use SoftDeletes;

    #[Inject]
    protected ModelPolicy $modelPolicy;

    public const ENABLE = 1;

    public const DISABLE = 0;

    /**
     * 数据权限过滤
     * @param $query
     * @param int|null $uid
     * @return Builder
     */
    public function scopeWithPermission($query, ?int $uid = null): Builder
    {
        return $this->modelPolicy->apply($query, $this, $uid);
    }

    /**
     * The table associated with the model.
     */
    protected ?string $table = 'ch_robot';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'uid', 'main_uid', 'nickname', 'acctid', 'alias', 'real_name', 'remark', 'avatar', 'mobile', 'corp_id', 'corp_name', 'corp_wxid', 'user_id', 'userid', 'login_status', 'first_login_time', 'login_time', 'login_out_time', 'group_id', 'pro_code', 'pro_name', 'city_code', 'city_name', 'server_pro_code', 'server_city_code', 'server_pro_name', 'server_city_name', 'is_yd', 'seat_id', 'robot_key', 'server_id', 'contact_num', 'room_num', 'is_auto_renewal', 'uuid', 'ht_bind_status', 'ht_seat_id', 'is_api', 'deleted_at', 'created_at', 'updated_at', 'gender'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'uid' => 'integer', 'main_uid' => 'integer', 'corp_id' => 'integer', 'login_status' => 'integer', 'group_id' => 'integer', 'is_yd' => 'integer', 'seat_id' => 'integer', 'server_id' => 'integer', 'contact_num' => 'integer', 'room_num' => 'integer', 'is_auto_renewal' => 'integer', 'ht_bind_status' => 'integer', 'ht_seat_id' => 'integer', 'is_api' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'gender' => 'integer'];

    public function groups(): BelongsToMany
    {
        return $this->belongsToMany(ChRobotGroup::class, 'ch_robot_group_map', 'robot_id', 'group_id');
    }

    public function user(): HasOne
    {
        return $this->hasOne(ChUser::class, 'id', 'uid');
    }

    public function roomInviteAcceptConfig(): HasOne
    {
        return $this->hasOne(ChRoomInviteAcceptConfig::class, 'robot_id', 'id');
    }

    public function server()
    {
        return $this->hasOne(ChServer::class, 'id', 'server_id');
    }
}
